import { createRouter, createWebHistory } from 'vue-router';
import HomeView from '../views/HomeView.vue';
import CityDetailView from '../views/CityDetailView.vue';
import FavoritesView from '../views/FavoritesView.vue';
import SearchView from '../views/SearchView.vue';
import ContactView from '../views/ContactView.vue';

const router = createRouter({
  history: createWebHistory(),
  routes: [
    {
      path: '/',
      name: 'home',
      component: HomeView,
      meta: {
        title: '环球旅行指南 - 探索世界名城'
      }
    },
    {
      path: '/city/:id',
      name: 'city-detail',
      component: CityDetailView,
      meta: {
        title: '城市详情 - 环球旅行指南'
      }
    },
    {
      path: '/favorites',
      name: 'favorites',
      component: FavoritesView,
      meta: {
        title: '我的收藏 - 环球旅行指南'
      }
    },
    {
      path: '/search',
      name: 'search',
      component: SearchView,
      meta: {
        title: '搜索结果 - 环球旅行指南'
      }
    },
    {
      path: '/contact',
      name: 'contact',
      component: ContactView,
      meta: {
        title: '联系我们 - 环球旅行指南'
      }
    },
    {
      path: '/:pathMatch(.*)*',
      name: 'not-found',
      component: () => import('../views/NotFoundView.vue'),
      meta: {
        title: '页面不存在 - 环球旅行指南'
      }
    }
  ],
  scrollBehavior(to, from, savedPosition) {
    if (savedPosition) {
      return savedPosition;
    } else {
      return { top: 0 };
    }
  }
});

router.beforeEach((to, from, next) => {
  document.title = to.meta.title || '环球旅行指南';
  next();
});

export default router;

